using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace AdvancedInspector
{
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field)]
	public class TitleAttribute : Attribute, IRuntimeAttribute, IRuntimeAttribute<TitleAttribute>
	{
		public delegate TitleAttribute TitleDelegate();

		private string message;

		private FontStyle style = FontStyle.Bold;

		private string methodName = string.Empty;

		private List<Delegate> delegates = new List<Delegate>();

		public string Message
		{
			get
			{
				return message;
			}
			set
			{
				message = value;
			}
		}

		public FontStyle Style
		{
			get
			{
				return style;
			}
			set
			{
				style = value;
			}
		}

		public string MethodName
		{
			get
			{
				return methodName;
			}
		}

		public Type Template
		{
			get
			{
				return typeof(TitleDelegate);
			}
		}

		public List<Delegate> Delegates
		{
			get
			{
				return delegates;
			}
			set
			{
				delegates = value;
			}
		}

		public TitleAttribute(string methodName)
		{
			this.methodName = methodName;
		}

		public TitleAttribute(FontStyle style, string message)
		{
			this.style = style;
			this.message = message;
		}

		public TitleAttribute(Delegate method)
		{
			delegates.Add(method);
		}

		public TitleAttribute Invoke(int index)
		{
			if (delegates.Count == 0 || index >= delegates.Count)
			{
				return this;
			}
			try
			{
				return delegates[0].DynamicInvoke() as TitleAttribute;
			}
			catch (Exception innerException)
			{
				if (innerException is TargetInvocationException)
				{
					innerException = ((TargetInvocationException)innerException).InnerException;
				}
				Debug.LogError(string.Format("Invoking a method failed while trying to retrieve a Title attribute. The exception was \"{0}\".", innerException.Message));
				return null;
			}
		}
	}
}
